
from flask import Flask
from flask import json,jsonify
from flask import Response
from flask import request
import  importlib
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello World!'

@app.route('/call/<module>/<method>',methods = ['GET','POST'])
def call(module,method):
    print module
    print method
    m = __import__( 'libs.msb',fromlist=[])
    target_func = getattr(m, 'test')
    target_func()
    try:
        print request.form['b']

    except Exception as e:
        print e
    print request.args.get('a')
    print request.method  # 'GET'
    print request.get_json()

    if request.headers['Content-Type'] == 'text/plain':
        return "Text Message: " + request.data

    if request.headers['Content-Type'] == 'application/json':
        return "JSON Message: " + json.dumps(request.json)

    data = {
        'hello': 'world',
        'number': 3
    }
    js = json.dumps(data)

    resp = Response(js, status=200, mimetype='application/json')
    resp.headers['Link'] = 'http://luisrei.com'

    return resp

    # return commandPath

@app.errorhandler(404)
def not_found(error=None):
    message = {
            'status': 404,
            'message': 'Not Found: ' + request.url,
    }
    resp = jsonify(message)
    resp.status_code = 404

    return resp

if __name__ == '__main__':
    # app.run(host='0.0.0.0')
    #app.run(debug=True)
    #m = __import__('msb2')
    #m = __import__('libs', fromlist=['msb'])
    m = importlib.import_module('libs.msb')
    m = importlib.import_module('.msb','libs')
    target_func = getattr(m, 'test')
    print getattr(m, 'a')
    target_func()
    exec "import sys\nprint sys.path"







